package org.jeecg.modules.xl.kuaimai.util;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author nanmian
 */
public class RopUtils {

    public static final String AppKey = "1433178357";
    public static final String AppSecret = "eaf2ddde2df049e3ad95d08352655452";
    public static final String AccessToken = "636c53525e6f441099e0db65af684c74";

    public static void main(String[] args) throws Exception {
        Map<String, String> params = Maps.newHashMap();
        params.put("appKey", AppKey);
        params.put("format", "json");
        //params.put("method", "open.system.time.get");

        params.put("method",
                "stock.api.status.query");
//        params.put("pageNo", "2");
//        params.put("pageSize", "10");
        params.put("session", AccessToken);
       // params.put("status", "FINISH");
        params.put("timestamp", "2024-11-06 19:50:11");
        params.put("version", "1.0");
        params.put("sign_method", "md5");
        String sign = serverSign(params, AppSecret, "stock.api.status.query");
        System.out.println("加密之后的签名：" + sign);
        params.put("sign", sign);

        String result = HttpUtil.post("https://gw3.superboss.cc/router", JSON.toJSONString(params));
        System.out.println("返回结果数据：" + JSON.toJSONString(result));
    }

    /**
     * 使用<code>secret</code>对paramValues按以下算法进行签名： <br/>
     * uppercase(hex(sha1(secretkey1value1key2value2...secret))
     *
     * @param paramValues 参数列表
     * @param secret
     * @return
     */
    public static String serverSign(Map<String, String> paramValues, String secret) throws Exception {
        return serverSign(paramValues, Lists.newArrayList("logger"), secret, RopAppSecretSignUtils.SIGN_METHOD_MD5);
    }

    /**
     * 使用<code>secret</code>对paramValues按以下算法进行签名： <br/>
     * uppercase(hex(sha1(secretkey1value1key2value2...secret))
     *
     * @param paramValues 参数列表
     * @param secret
     * @param signMethod  签名方法，目前支持：空（老md5)、md5, hmac_md5, hmac-sha256 三种
     * @return
     */
    public static String serverSign(Map<String, String> paramValues, String secret, String signMethod) throws Exception {
        return serverSign(paramValues, Lists.newArrayList("logger"), secret, signMethod);
    }

    /**
     * 对paramValues进行签名，其中ignoreParamNames这些参数不参与签名
     *
     * @param paramValues
     * @param ignoreParamNames
     * @param secret
     * @return
     */
    public static String serverSign(Map<String, String> paramValues, List<String> ignoreParamNames, String secret, String signMethod) throws Exception {
        try {
            //去除需要忽略不参与签名的参数
            if (ignoreParamNames != null && ignoreParamNames.size() > 0) {
                for (String ignoreParamName : ignoreParamNames) {
                    paramValues.remove(ignoreParamName);
                }
            }

            String sign;
            if (RopAppSecretSignUtils.SIGN_METHOD_HMAC.equals(signMethod)) {
                sign = RopAppSecretSignUtils.signTopRequest(paramValues, secret, RopAppSecretSignUtils.SIGN_METHOD_HMAC);
            } else if (RopAppSecretSignUtils.SIGN_METHOD_HMAC_SHA256.equals(signMethod)) {
                sign = RopAppSecretSignUtils.signTopRequest(paramValues, secret, RopAppSecretSignUtils.SIGN_METHOD_HMAC_SHA256);
            } else {
                sign = RopAppSecretSignUtils.signTopRequest(paramValues, secret, RopAppSecretSignUtils.SIGN_METHOD_MD5);
            }

            return sign;
        } catch (IOException e) {
            throw new Exception(e);
        }
    }

    public static String getUUID() {
        UUID uuid = UUID.randomUUID();
        return uuid.toString().toUpperCase();
    }

    /**
     * 获取文件的真实后缀名。目前只支持JPG, GIF, PNG, BMP四种图片文件。
     *
     * @param bytes 文件字节流
     * @return JPG, GIF, PNG or null
     */
    public static String getFileSuffix(byte[] bytes) {
        if (bytes == null || bytes.length < 10) {
            return null;
        }

        if (bytes[0] == 'G' && bytes[1] == 'I' && bytes[2] == 'F') {
            return "GIF";
        } else if (bytes[1] == 'P' && bytes[2] == 'N' && bytes[3] == 'G') {
            return "PNG";
        } else if (bytes[6] == 'J' && bytes[7] == 'F' && bytes[8] == 'I' && bytes[9] == 'F') {
            return "JPG";
        } else if (bytes[0] == 'B' && bytes[1] == 'M') {
            return "BMP";
        } else {
            return null;
        }
    }

    /**
     * 获取文件的真实媒体类型。目前只支持JPG, GIF, PNG, BMP四种图片文件。
     *
     * @param bytes 文件字节流
     * @return 媒体类型(MEME - TYPE)
     */
    public static String getMimeType(byte[] bytes) {
        String suffix = getFileSuffix(bytes);
        String mimeType;

        if ("JPG".equals(suffix)) {
            mimeType = "image/jpeg";
        } else if ("GIF".equals(suffix)) {
            mimeType = "image/gif";
        } else if ("PNG".equals(suffix)) {
            mimeType = "image/png";
        } else if ("BMP".equals(suffix)) {
            mimeType = "image/bmp";
        } else {
            mimeType = "application/octet-stream";
        }

        return mimeType;
    }
}

